<?php
require '../inc/inc.php';


$secretKey = 'ERWIx22Ym7';//密钥
$allowTimeInterval = 500;// 允许平台发送请求时间到接收数据的时间间隔(单位:秒)
//var_export($_COOKIE);
//var_export($_SESSION);
//exit;

if( ($_GET['op']=='login') ){
	
	/* 验证,是否真的已正确登录(是否平台正常转发) */
	$username	= $_GET['username'];
	$timestamp	= $_GET['timestamp'];
	$code		= $_GET['code'];
	
	// 请求时间太长
	if (($_SERVER['REQUEST_TIME'] - $timestamp) > $allowTimeInterval) {
		header('Location:'.$loginUrl);
		exit;
	}
	
	$calcCode	= md5(md5($username.md5($secretKey)).md5($timestamp));
	// 校验码不正确
	if ($calcCode != $code) {
		header('Location:'.$loginUrl);
		exit;
	}
	// 验证通过了是否还需要去平台上再次验证?
	
	// 如果用户不存在,添加到数据库
	if (!User::isUserExists($username)) {
		//确认认成功 新建用户并登录
		$ret = User::create($username);
	} else {
		$ret = User::login($username);
	}
	if ($ret) {
		if (empty($_SESSION['created_role'])) {
			$param = '?createRole=1';
		} else {
			$param = '';
		}
		header('Location:http://mc2.new/client/mc2.html'.$param);
//		header('Location:http://192.168.1.103/mc2.new/webroot/client/mc2.html'.$param);
//		var_export($_SESSION);
	} else {
		var_dump($ret);
		var_export($DB->errors);
	}
	exit;
//} elseif ($_GET['op']=='login') {
	
} else {
	header('Location:/index.php');
	exit;
}

